package main

import (
	"context"
	"log"
	"time"

	pb "neu/liuhy/gorpc/proto"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	// 连接到gRPC服务器
	conn, err := grpc.NewClient(":50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()

	// 创建客户端
	connection := pb.NewBookServiceClient(conn)

	// find book
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	r, err := connection.FindBook(ctx, &pb.BookRequest{Name: "book1", Id: "1", Author: "author1"})
	if err != nil {
		log.Fatalf("could not find book: %v", err)
	}
	log.Printf("Find book result: %s", r)

	// add book
	ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second)
	defer cancel2()

	addBookResult, err := connection.AddBook(ctx2, &pb.BookRequest{
		Name:   "book1",
		Id:     "1",
		Author: "author1",
	})
	if err != nil {
		log.Fatalf("could not greet batch: %v", err)
	}
	log.Println("Add book result: %v", addBookResult)

}
